package com.qeeyou.apps.android.craftsman.activity.publish;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.InstallMethodInfo;
import com.qeeyou.apps.android.craftsman.po.InstallTypeInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;
import com.qeeyou.apps.android.craftsman.view.wheelview.WheelView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstallTypeActivity extends Activity {

    private Button btnCancel;                       //取消按钮
    private Button btnConfirm;                      //确定按钮
    private WheelView wheelView;
    private CustomLoadingDialog mLoadingDialog;
    private List<InstallTypeInfo> typeList;         //方式列表
    private InstallTypeInfo typeInfo;               //安装方式

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intall_type);

        getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        setView();
        setEvent();
    }

    private void setView() {
        wheelView = (WheelView) this.findViewById(R.id.wheelView4InstallType);
        btnCancel = (Button) this.findViewById(R.id.btnCancel4InstallType);
        btnConfirm = (Button) this.findViewById(R.id.btnConfirm4InstallType);
    }

    private void setEvent() {
        if(typeList == null || typeList.size() == 0) {
            if(typeList == null) {
                typeList = new ArrayList<InstallTypeInfo>();
            }
            //填充
            List<String> itemList = new ArrayList<String>();
            itemList.add("标准");
            itemList.add("高级");
            wheelView.setItems(itemList, 0);
            new GetInstallTypeList().execute(new String[] {"2"});
        } else {
            List<String> itemList = new ArrayList<String>();
            for(int i = 0, n = typeList.size(); i < n; i++) {
                InstallTypeInfo typeInfo = typeList.get(i);
                itemList.add(typeInfo.getTypename());
            }
            wheelView.setItems(itemList, 0);
            wheelView.setOnItemSelectedListener(onItemSelectedListener);
        }
        btnCancel.setOnClickListener(onCancelClickListener);
        btnConfirm.setOnClickListener(onConfirmClickListener);
    }


    //返回按钮
    private View.OnClickListener onCancelClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    InstallTypeActivity.this.finish();
                }
            }, 500);
        }
    };

    //确定按钮
    private View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(typeInfo != null) {
                Intent intent = new Intent();
                intent.putExtra("install_type", typeInfo);
                InstallTypeActivity.this.setResult(RESULT_OK, intent);
            }
            InstallTypeActivity.this.finish();
        }
    };


    private WheelView.OnItemSelectedListener onItemSelectedListener = new WheelView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(int selectedIndex, String item) {
            if(typeList != null) {
                typeInfo = typeList.get(selectedIndex);
            } else {
                typeInfo = null;
            }
        }
    };

    class GetInstallTypeList extends AsyncTask<String, Void, List<InstallTypeInfo>> {

        public GetInstallTypeList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(InstallTypeActivity.this);
            }
            mLoadingDialog.setMessage("正在获取安装类型列表...");
            mLoadingDialog.show();
        }

        @Override
        protected List<InstallTypeInfo> doInBackground(String... params) {
            String type = params[0];
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("releasetype", type);
            List<InstallTypeInfo> typelList = AppService.getInstance().getInstallTypeList(paramMap);
            return typelList;
        }

        @Override
        protected void onPostExecute(List<InstallTypeInfo> typelList) {
            super.onPostExecute(typelList);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if(typelList != null && !typelList.isEmpty()) {
                InstallTypeActivity.this.typeList.clear();
                InstallTypeActivity.this.typeList.addAll(typelList);
                List<String> titleList = new ArrayList<String>();
                for(int i = 0, n = typelList.size(); i < n; i++) {
                    titleList.add(typelList.get(i).getTypename());
                }
                wheelView.setItems(titleList, 0);
                typeInfo = InstallTypeActivity.this.typeList.get(0);
                wheelView.setIsLoop(false);
                wheelView.setOnItemSelectedListener(onItemSelectedListener);
            }

        }
    }


}
